<template>
	<el-container>
		<div v-show="screenVisible" class="table-screen-column">
			<el-form label-width="auto" label-position="right">
				<el-row :gutter="20">
				  <el-col :span="4" >
				  	<el-form-item label="状态">
						<el-select v-model="search.status" clearable placeholder="状态">
  						  	<el-option label="成功" value="1"></el-option>
  						  	<el-option label="失败" value="0"></el-option>
  						</el-select>
					</el-form-item>
				  </el-col>
				  <el-col :span="4">
				  	<el-form-item label="系统设备">
                        <el-input v-model="search.os" placeholder="系统设备" clearable></el-input>
    				</el-form-item>
				  </el-col>
				  <el-col :span="6">
				  	<el-form-item label="登录时间">
                        <el-date-picker v-model="time_stamp" type="datetimerange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期"
                         value-format="x" @change="changeTimeStamp"></el-date-picker>
    				</el-form-item>
				  </el-col>
				</el-row>
			</el-form>
		</div>
		<el-header>
			<div class="left-panel"></div>
			<div class="right-panel">
				<div class="right-panel-search">
					<el-input v-model="search.username" placeholder="账户" clearable></el-input>
					<el-input v-model="search.ip" placeholder="ip地址" clearable></el-input>
					<el-button type="primary" icon="el-icon-search" plain @click="handleQuery">搜索</el-button>
					<el-button @click="resetQuery" icon="el-icon-refresh">重置</el-button>
					<el-button type="primary" link @click="screenVisible = !screenVisible">
            		  {{ screenVisible ? '关闭更多筛选' : '显示更多筛选'}}
            		  <el-icon><el-icon-arrow-down v-if="screenVisible" /><el-icon-arrow-up v-else /></el-icon>
            		</el-button>
				</div>
			</div>
		</el-header>
		<el-main class="nopadding">
			<el-table ref="table" v-loading="loading" :data="tableList" @selection-change="selectionChange" border>
				<el-table-column label="#" prop="id" width="100" align="center"></el-table-column>
				<el-table-column label="账户" prop="username"></el-table-column>
				<el-table-column label="系统设备" prop="os" width="140"></el-table-column>
				<el-table-column label="浏览器" prop="browser" width="140"></el-table-column>
				<el-table-column label="IP地址" prop="ip" width="140"></el-table-column>
				<el-table-column label="所在地址" prop="ip_location"></el-table-column>
				<el-table-column label="状态" prop="status" width="150">
					<template #default="scope">
						<el-tag v-if="scope.row.status==1" type="success">成功</el-tag>
						<el-tag v-if="scope.row.status==0" type="danger">失败</el-tag>
					</template>
				</el-table-column>
                <el-table-column label="提示消息" prop="message"></el-table-column>
				<el-table-column label="备注" prop="remark"></el-table-column>
				<el-table-column label="登录时间" prop="login_time" width="180"></el-table-column>
			</el-table>
		</el-main>
		<el-footer>
			<sc-page v-model="search"></sc-page>
		</el-footer>
	</el-container>

</template>

<script>
	export default {
		name: 'loginlog',
		components: { },
		data() {
			return {
				loading: true,
				screenVisible: false,
				tableList: [],
      			total: 0,
				selection: [],
				search: {
					page: 1,
        			pageSize: 20,
					total: 0,
					paging: 0,
        			username: undefined,
        			ip: undefined,
        			os: undefined,
        			status: undefined,
                    time_stamp: ''
				},
                time_stamp: [],
			}
		},
		mounted() {
			this.handleQuery()
		},
		watch: {
        	"search.paging": function(e) {
				if(e) {
					this.getTableList()
				}
			},
		},
		methods: {
			//获取列表
			async getTableList() {
				this.loading = true
				var res = await this.$API.system.log.loginList.get(this.search)
				this.loading = false
				if(res.code == 200) {
					this.tableList = res.data.rows
					this.search.total = res.data.total
				} else {
					this.$alert(res.message, "提示", {type: 'error'})
				} 
			},
			//搜索
			handleQuery() {
				this.getTableList()
			},
			//重置搜索
			resetQuery() {
				this.search = {
        			username: undefined,
        			ip: undefined,
        			os: undefined,
        			status: undefined,
                    time_stamp:''
				}
                this.time_stamp = []
				this.handleQuery()
			},
			//添加
			add(){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('add', this.search)
				})
			},
			//编辑
			table_edit(row){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('edit').setData(row, this.search)
				})
			},
			//查看
			table_show(row){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('show').setData(row, this.search)
				})
			},
			//查看
			table_password(row){
				this.dialog.password = true
				this.$nextTick(() => {
					this.$refs.passwordDialog.open('add').setData(row, this.search)
				})
			},
			//删除
			async table_del(row){
				var reqData = {id: row.id}
				var res = await this.$API.agent.users.delete.post(reqData);
				if(res.code == 200){
					this.handleQuery()
					this.$message.success("删除成功")
				}else{
					this.$alert(res.message, "提示", {type: 'error'})
				}
			},
			//批量删除
			async batch_del(){
				this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？如果删除项中含有子集将会被一并删除`, '提示', {
					type: 'warning'
				}).then(() => {
					const loading = this.$loading();
					this.handleQuery()
					loading.close();
					this.$message.success("操作成功")
				}).catch(() => {

				})
			},
			//表格选择后回调事件
			selectionChange(selection){
				this.selection = selection;
			},
			//本地更新数据
			handleSaveSuccess(mode, search){
				if(mode !='show') {
					this.search = search
					this.handleQuery()
				}
			},
            //时间搜素
            changeTimeStamp(e) {
                this.search.time_stamp = e.toString()
            }
		}
	}
</script>

<style>
  .el-row {
    margin-bottom: 20px;
    &:last-child { margin-bottom: 0;}
  }
  .el-col {border-radius: 4px;}
  .bg-purple-dark {background: #99a9bf;}
  .bg-purple {background: #d3dce6;}
  .bg-purple-light {background: #e5e9f2;}
  .grid-content {border-radius: 4px;min-height: 36px;}
  .row-bg {padding: 10px 0;background-color: #f9fafc;}
</style>
